﻿@model IEnumerable<BenzInfo.Hockey.Grenadiers.Models.Event>

@{
    ViewBag.Title = "Index";
}

<script type="text/javascript">

    $(document).ready(function () {

        $('#dialog_events').dialog({
            autoOpen: false,
            modal: true,
            draggable: false,
            resizable: false,
            width: 580,
            height: 'auto',
            buttons: [
                {
                    id: 'dialog_btn_save', text: '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Save',
                    click: function () {
                        $('form', this).submit();
                    }
                },
                {
                    id: 'dialog_btn_cancel', text: '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Cancel',
                    click: function () {
                        $(this).dialog('close');
                    }
                }
            ]
        });

        $('#calendar_events').fullCalendar({
            header: {
                left: 'title',
                right: 'today prev,next'
            },
            theme: true,
            timeFormat: 'H(:mm)',
            buttonText: {
                today: '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Today'
            },
            monthNames: @Html.Raw(BenzInfo.Hockey.Grenadiers.Resources.Dictionary.MonthNames),
            monthNamesShort: @Html.Raw(BenzInfo.Hockey.Grenadiers.Resources.Dictionary.MonthNamesShort),
            dayNames: @Html.Raw(BenzInfo.Hockey.Grenadiers.Resources.Dictionary.DayNames),
            dayNamesShort: @Html.Raw(BenzInfo.Hockey.Grenadiers.Resources.Dictionary.DayNamesShort),
            events: function(start, end, callback) {
                $.post('Queries/GetEvents',
                    {
                        start: Math.round(start.getTime() / 1000),
                        end: Math.round(end.getTime() / 1000)
                    },
                    function(data) {

                        $.each(data, function(i, item) {
                            item.start = JsonDate(item.start);
                            item.end = JsonDate(item.end);
                            return true;
                        });

                        callback(data);

                    }, 'json'
                );
            },
            dayClick: function(date) {
                if ("@(ViewBag.CurrentUser != null)" == "True")
                {
                    var url = 'Events/Create';
                    var title = '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Create';

                    $('#dialog_events').empty().load(url, function () {
                        $('#dialog_events').dialog('option', 'title', title).dialog('open');
                    });
                }
            },
            eventClick: function(event) {
                if ("@(ViewBag.CurrentUser != null)" == "True")
                {            
                    var url = event.type + 's/Edit/' + event.id;
                    var title = '@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Edit';

                    $('#dialog_events').empty().load(url, function () {
                        $('#dialog_events').dialog('option', 'title', title).dialog('open');
                    });
                }
            },
            eventRender: function(event, element) {
                element.qtip({
                    style: {  background : '#000042', width: 432 },
                    adjust: {screen: true },
                    position: { corner: { tooltip: 'topMiddle', target: 'bottomMiddle' } },
                    hide: { when: 'mouseout', fixed: true },
                    content: {
                        url: event.type + 's/Qtip/' + event.id
                    }
                });
            }
        });
    });

    function JsonDate(jsonDate) {
        return new Date(parseInt(jsonDate.substr(6)));
    }

    function FormatDateTime(date) {
        return $.datepicker.formatDate('yy-mm-dd', date) + ' ' + $.datepicker.formatTime('H(:mm)', date);
    }

</script>

<h1>@(BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Event)s</h1>

<div id="calendar_events"></div>

<h2>@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Legend</h2>
<ul id="calendar_legend">
    <li><span style="background-color:#700706;"></span>@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.HomeGames</li>
    <li><span style="background-color:#000042;"></span>@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.AwayGames</li>
    <li><span style="background-color:#d99d2f;"></span>@BenzInfo.Hockey.Grenadiers.Resources.Dictionary.Events</li>
</ul>
<div class="clear"></div>
<div id="dialog_events"></div>